#include "fun_task.h"

uint8_t Set_time_fail  = pdFALSE;
uint8_t low_power_state = pdFALSE;
static TaskHandle_t               fun_task_Handle = NULL;
static QueueHandle_t              fun_msg_queue = NULL;

charge_state_t charge_state;

// static void fun_display_bat_power_charge(uint8_t *st)
// {
// 	valva_aerofluxus();
// }

void fun_task_state_check(task_status_t *state)
{
    EventBits_t bits  = 0;

    bits = xEventGroupGetBits(get_evemt_handle());
    if( (bits & BREAST_PUMP_PAUSE_BIT ) == BREAST_PUMP_PAUSE_BIT )
    {
        *state = BREAST_PUMP_PAUSE;
    }
    else if( (bits & BREAST_PUMP_START_BIT ) == BREAST_PUMP_START_BIT )
    {
        *state = BREAST_PUMP_START;
    }
    else if( (bits & BREAST_BAT_POWER_LOW_BIT ) == BREAST_BAT_POWER_LOW_BIT )
    {
        *state = BREAST_BAT_POWER_LOW;
    }
    else if( (bits & BREAST_BAT_CHARGE_BIT ) == BREAST_BAT_CHARGE_BIT )
    {
        *state = BREAST_BAT_CHARGE;
    }
    else if( (bits & BREAST_TIM_SET_BIT ) == BREAST_TIM_SET_BIT )
    {
        *state = BREAST_TIM_SET;
    }
}

void fun_task(void *args)
{
    led_packet_t *led_msg = NULL;
    uint32_t addr = 0;
    uint8_t last_bat;
    uint16_t bat_check_tick = 0;
    uint8_t bat = 0xf0;
//    uint16_t systeam_idle_tick = 0;
    uint8_t low_power_tick = 0;
    EventGroupHandle_t event = NULL;
    EventBits_t uxBits = 0;
    EventBits_t bits  = 0;
    Set_time_fail = pdFALSE;
//    uint8_t batcnt[3] = {0};
    //获取事件句柄
    event = get_evemt_handle();
    if(xQueueReceive(fun_msg_queue,&addr,portMAX_DELAY) !=  pdFALSE)
    {
        //获取msg指针
        led_msg = (led_packet_t *)addr;
    }
    
    while(1)
    {
        bits = xEventGroupGetBits(event);  
        if( (bits & BREAST_SLEEP_BIT ) != BREAST_SLEEP_BIT )
        {
            //获取状态
            fun_task_state_check(&led_msg->task_status);

            //bat为f0时说明时首次上电
            if(bat == 0xf0) 
            {
                bat = led_msg->bat_st;
            }
            //检测充电插入
            if(GET_CHARGE_CHECK_CHRG() == 0) 
            {
                if(charge_state == CHARGE_OFF) 
                {
                    //插入时需要放气
                    VALVA_AEROFLUXUS_START();
                }

                xEventGroupClearBits(event, BREAST_SYS_ALL_BIT);
                xEventGroupSetBits(event,BREAST_BAT_CHARGE_BIT);
                charge_state = CHARGE_ON;
                low_power_state = pdFALSE;
            }
            else 
            {   
                //充满/充电器拔出就休眠
                if(charge_state == CHARGE_ON) 
                {
                    xEventGroupClearBits(event,BREAST_SYS_ALL_BIT);
                    xEventGroupSetBits(event,BREAST_SLEEP_BIT);
                    led_msg->systeam_tick = 0;
                    charge_state = CHARGE_OFF;
                }
                else 
                {
                    charge_state = CHARGE_OFF;
                }
            }

            switch(led_msg->task_status) 
            {
            case BREAST_PUMP_START: 
                {    
                    bat_check_tick++;
                    if(bat_check_tick >= 60) //180
                    {
                        uxBits = xEventGroupGetBits(event);
                        if((uxBits & (VALVA_STATE_EVENT_BIT)) == (VALVA_STATE_EVENT_BIT))
                        {  
                            uint8_t temp_bat = bat_led_gear(sliding_average_filter(get_bat_volt()+BAT_OFFSET,&bat_check_tick));
                            if(temp_bat != 0xff) 
                            { 
                                if(temp_bat != 0x0f) 
                                {
                                    
                                    if(last_bat == 0) 
                                    {
                                        last_bat = temp_bat;
                                    }
                                    
                                    if((last_bat - temp_bat) > 0) 
                                    {
                                        last_bat = temp_bat;
                                    } 
                                    else 
                                    {
                                        temp_bat = last_bat;
                                    }
                                    
                                    bat = temp_bat;
                                    led_msg->bat_st =  (bat_st_t)temp_bat;
                                    if(bat == 1)
                                    {
                                       led_display_LEVEL(LOW_POW_DIS_LEVEL);
                                    }
                                } 
                                else 
                                {
                                    led_msg->systeam_tick = 0;
                                    low_power_state = pdTRUE;
                                    //切换为低压告警状态
                                    xEventGroupClearBits(event, BREAST_SYS_ALL_BIT);
                                    xEventGroupSetBits(event,BREAST_BAT_POWER_LOW_BIT);
                                    break;
                                }
                            }
                        }
                    }

                    led_msg->systeam_tick++;
                    led_msg->task_tens= (uint8_t)(led_msg->systeam_tick/6000);
                    led_msg->task_units= (uint8_t)((led_msg->systeam_tick/600)%10);
                    if(led_msg->systeam_tick > (led_msg->task_min_set * 600)) 
                    {  
                        xEventGroupClearBits(event,BREAST_SYS_ALL_BIT);
                        xEventGroupSetBits(event,BREAST_SLEEP_BIT);
                    }
                    break;
                }
                
            case BREAST_PUMP_PAUSE: 
                {
                    //待机，5分钟后休眠
                     led_msg->tim_set_timeout++;
                    if( led_msg->tim_set_timeout > SYS_SLEEP_HALT_TIME)
                    {
                         led_msg->tim_set_timeout = 0;
                        xEventGroupClearBits(event,BREAST_SYS_ALL_BIT);
                        xEventGroupSetBits(event,BREAST_SLEEP_BIT);
                    }
                    break;
                }
                //充电中    
            case BREAST_BAT_CHARGE: 
                {
                    uint16_t volt = 0;

                    volt = get_bat_volt();

                    volt -= 150;
                    bat = bat_led_gear(volt);
                    if(bat == 0x0f) 
                    {
                        bat = 0;
                    } 
                    if(bat == 0) 
                    {
                        bat = 1;
                    }
                    led_msg->bat_st = (bat_st_t)bat;
                    break;
                }
                //抵压提醒
            case BREAST_BAT_POWER_LOW: 
                {
                    low_power_tick++;
                    if(low_power_tick > SYS_SLEEP_LOW_POWER_TIME)
                    {
                        low_power_tick = 0;
                        xEventGroupSetBits(event,BREAST_SLEEP_BIT);
                    }
                    break;
                }
            //设置时间5S没操作，自动进入设置挡位模式，停止闪烁
            case BREAST_TIM_SET:
                {
                    led_msg->tim_set_timeout++;
                    if(led_msg->tim_set_timeout > SYS_TIME_SET_TIMEOUT_TIME)
                    {
                        Set_time_fail = pdTRUE;
                        led_msg->tim_set_timeout = 0;
                        xEventGroupClearBits(event,BREAST_TIM_SET_BIT);
                        xEventGroupSetBits(event,BREAST_PUMP_PAUSE_BIT);
                    }
                }
            }
        }
        vTaskDelay(100);
    }
}

int fun_led_packet(void *data)
{
    int error = pdFALSE;
    
    error = xQueueSend(fun_msg_queue,&data,portMAX_DELAY);
    return error;
}


void fun_task_init(void)
{
    //adc_config();
      
    fun_msg_queue = xQueueCreate(5,sizeof(led_packet_t*));   
    if(fun_msg_queue == NULL)
    {
        asm("nop");
        //SEGGER_RTT_printf(0,"Create fun_msg_queue error...\r\n");
    }
    
    if(xTaskCreate(fun_task,"fun_task",64,NULL,configMAX_PRIORITIES-2,&fun_task_Handle) == pdPASS)
    {
        asm("nop");
        //SEGGER_RTT_printf(0,"Create fun_task sucess...\r\n");
    }
    
}

void fun_task_deinit(void)
{
    vTaskDelete(fun_task_Handle);
   // vQueueDelete(fun_led_queue);
}














